package pwPlugin;

import java.io.Serializable;
import java.util.Vector;

public class Passwords implements Serializable {

  private static final long serialVersionUID = 1L;
  boolean activated;
  private Vector<Password> passwordlist;

  public Vector<Password> getPasswordlist() {
	return passwordlist;
  }

public Passwords() {
    this.passwordlist = new Vector<Password>();
  }

  public boolean update(Object pw) {
    if (pw instanceof Password) {
      ((Password) pw).update(((Password) pw).getUsername(),
          ((Password) pw).getPw(), ((Password) pw).getTags());
      this.passwordlist.setElementAt((Password) pw, passwordlist.indexOf(pw));
      return true;
    }
    return false;
  }

  public boolean add(Object newPw) {
    Password pw = (Password) newPw;
    if (this.passwordlist.size()==0) {
      System.out.println("[Passwords-ADD] Password with Tags "+pw.vecCharToString(pw.getTags())+" added!");
      this.passwordlist.add(pw);
    } else if(this.passwordlist.contains(pw)) {
      System.out.println("[Passwords-ADD] ERROR: Password with Tags "+pw.vecCharToString(pw.getTags())+" already in db!");
    } else {
      System.out.println("[Passwords-ADD] Password with Tags "+pw.vecCharToString(pw.getTags())+" added!");
      this.passwordlist.add(pw);
      return true;
    }
    return false;
  }

  public boolean delete(Object oldPw) {
    if (this.passwordlist.contains((Password) oldPw)) {
      System.out.println("DELETE: password deleted");
      this.passwordlist.remove((Password) oldPw);
      return true;
    } else {
      System.out.println("DELETE: no password found");
      return false;
    }
  }

  @Override
  public String toString() {
    int i = 0;
    String ret = new String();
    ret = "[";
    if (!(passwordlist == null) && !passwordlist.isEmpty()) {
      while (i < passwordlist.size()) {
        ret += passwordlist.elementAt(i).toString();
        i++;
      }
    }
    ret += "]";
    return ret;
  }

  public String getName() {
    return new String("Passwords");
  }

  public int getId() {
    return 3;
  }

  public Vector<?> getList() {
    if (this.passwordlist == null) {
      this.passwordlist = new Vector<Password>();
      return this.passwordlist;
    }
    return this.passwordlist;
  }
}
